/*
 * FrmMain.java
 */
package cconverter;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;

/**
 * The application's main frame.
 */
public class FrmMain extends FrameView {

    JFrame _frm = this.getFrame();
    public FrmUpdate _frmUpdate;
    public FrmOptions _frmOptions;
    public FrmViewList _frmViewList;
    public FrmAbout _frmAbout;
    public FrmQuickConverter _frmQuickConverter;
    public static Timer timer;
    public static TrayIcon trayIcon;
    public static Props props = new Props(); // properties
    public static Maps maps = new Maps();
    public static Sets _sets = new Sets();

    public FrmMain(SingleFrameApplication app) {
        super(app);

        initComponents();
        myInit();

        timer = new Timer();
        timer.scheduleAtFixedRate(new mainTimer(), 1, 1000);
        timer = new Timer();
        setTable();
        setTrayIcon();
        // set ComboBox
        cmbCurrency.setRenderer(new ComboBoxRender());
    }

    private void setTable() {
        DefaultTableModel dtm = (DefaultTableModel) table.getModel();
        for (int i = 0; i < dtm.getColumnCount(); i++) {
            table.getColumnModel().getColumn(i).setCellRenderer(new myRender());
            table.getColumnModel().getColumn(i).setCellRenderer(new iconRender());
        }
        // now, set table row height
        table.setRowHeight(22);
        table.setGridColor(Sets.grid);
        table.setShowHorizontalLines(true);
        table.setShowVerticalLines(false);
        table.setIntercellSpacing(new Dimension(0, 1));
    }

    private void setTrayIcon() {
        if (new Pxml().checkFile()) {
            String updatedate = "";
            try {
                updatedate = "Last update : " + new Pxml().getLastUpdate();
            } catch (Exception ex) {
                new FrmInfo(_frm, true).setInfo("System error", ex.getLocalizedMessage());
            }
            trayIcon.displayMessage("Currency converter", "Hi, I am here." + System.getProperty("line.separator") + updatedate, TrayIcon.MessageType.INFO);
        } else {
            trayIcon.displayMessage("No currency list", "Hi, I am here. " + System.getProperty("line.separator") + "Update currency list, please.", TrayIcon.MessageType.WARNING);
        }
    }

    class mainTimer extends TimerTask {

        public void run() {
            if (!Maps._vmap.isEmpty()) { // if maps loaded
                btnConvert.setEnabled(true);
                btnConvert.setToolTipText("Click to convert");
                mniViewList.setEnabled(true);
                tbViewList.setEnabled(true);
                trayViewList.setEnabled(true);
                mniQuickConverter.setEnabled(true);
                tbQuickConverter.setEnabled(true);
            }

            // set last update label
            showLastUpdate();
            // check options
            checkOptions();
        }
    }

    /*
     *      ACTIONS
     */
    @Action
    public void showUpdate() {
        if (_frmUpdate == null) {
            _frmUpdate = new FrmUpdate();
            _frmUpdate.setVisible(true);
        } else {
            _frmUpdate.setVisible(true);
        }
    }

    @Action
    public void showOptions() {
        if (_frmOptions == null) {
            _frmOptions = new FrmOptions();
            _frmOptions.setVisible(true);
        } else {
            _frmOptions.setVisible(true);
        }
    }

    @Action
    public void showViewList() {
        if (_frmViewList == null) {
            _frmViewList = new FrmViewList();
            _frmViewList.setVisible(true);
        } else {
            _frmViewList.setVisible(true);
        }
    }

    @Action
    public void showAbout() {
        if (_frmAbout == null) {
            _frmAbout = new FrmAbout();
            _frmAbout.setVisible(true);
        } else {
            _frmAbout.setVisible(true);
        }
    }

    @Action
    public void showQuickConverter() {
        if (_frmQuickConverter == null) {
            _frmQuickConverter = new FrmQuickConverter();
            _frmQuickConverter.setVisible(true);
        } else {
            _frmQuickConverter.setVisible(true);
        }
    }

    @Action
    public void clean() {
        txtAmount.setText(null);
        cmbCurrency.setSelectedIndex(0);
        DefaultTableModel dtm = (DefaultTableModel) table.getModel();
        dtm.getDataVector().clear();
        table.setModel(dtm);
        table.repaint();
    }

    @Action
    public void Convert() {
        if (validate()) {
            Map vmap = Maps._vmap;
            Map cmap = Maps._cmap;
            double amount = Double.parseDouble(txtAmount.getText());
            String currency = (String) cmbCurrency.getSelectedItem();
            currency = currency.substring(currency.length() - 4, currency.length() - 1);
            double var = Double.parseDouble(vmap.get(currency).toString());
            double toeuro = amount / var;
            // fill a table
            DefaultTableModel dtm = (DefaultTableModel) table.getModel();
            dtm.getDataVector().clear();

            // add sortReOrder //


            TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(dtm);
            table.setRowSorter(sorter);

            ArrayList arrl = new ArrayList();
            arrl.addAll(cmap.keySet());
            Collections.sort(arrl);
            for (Object one : arrl) {
                double racun = toeuro * Double.parseDouble(vmap.get(one).toString());
                Object[] obj = new Object[3];
                obj[0] = new Flags().Flags(one.toString().toLowerCase());
                obj[1] = cmap.get(one) + " (" + one + ")";
                obj[2] = new BigDecimal(racun).setScale(Integer.parseInt(props.getProp("decimalPlaces", "2")), BigDecimal.ROUND_HALF_UP);
                dtm.addRow(obj);
            }
            table.setModel(dtm);
            mniExportToCSV.setEnabled(true);
        }
    }

    public boolean validate() {
        boolean ret = true;
        if (!isNumber(txtAmount.getText()) || txtAmount.getText().length() == 0) {
            new FrmInfo(_frm, true).setInfo("Error", "Enter amount for converting...");
            return false;
        }
        return ret;
    }

    public boolean isNumber(String num) {
        boolean ret = true;
        for (int i = 0; i < num.length(); i++) {
            if (Character.isDigit(num.charAt(i))) {
                ret = true;
            } else {
                ret = false;
            }
        }
        return ret;
    }

    @Action
    public void exportTxt() {
        JFileChooser fileC = new JFileChooser(File.separator);
        //fileC.setApproveButtonText("Save .CSV");
        fileC.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileC.setDialogTitle("Save .CSV");
        int rez = fileC.showSaveDialog(null);
        if (rez == JFileChooser.APPROVE_OPTION) {
            try {
                this.makeExportFile(fileC.getSelectedFile().getAbsolutePath() + ".csv", this.makeExportContentTxt());
                trayIcon.displayMessage("Currency converter", "Table content exported to .csv file", TrayIcon.MessageType.NONE);
            } catch (IOException ex) {
                new FrmInfo(_frm, true).setInfo("Error", ex.getLocalizedMessage());
            }
        }
    }

    public void makeExportFile(String filePath, String content) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(filePath));
        bw.write(content);
        bw.close();
    }

    public String makeExportContentTxt() {
        String ret = "Curency;Rate" + Sets.newline;
        TableModel model = table.getModel();
        for (int i = 0; i < model.getRowCount(); i++) {
            for (int j = 1; j < model.getColumnCount(); j++) {
                ret += model.getValueAt(i, j).toString().replace(".", ",") + ";";
            }
            ret += Sets.newline;
        }
        ret += (String) "Convert " + txtAmount.getText() + " " + cmbCurrency.getSelectedItem() + " to other.";
        return ret;
    }

    public void checkOptions() {
        try {
            props.loadProperties();
        } catch (Exception ex) {
            new FrmInfo(_frm, true).setInfo("Error", ex.getLocalizedMessage());
        }

        // if Always on top set frame
        if (props.getProp("isAOT", "false").equals("true")) {
            _frm.setAlwaysOnTop(true);
        } else {
            _frm.setAlwaysOnTop(false);
        }
        // if autoupdate set try to download new currency list
        if (props.getProp("isAU", "false").equals("true")) {
            try {
                new Pxml().makeFile();
            } catch (Exception ex) {
                new FrmInfo(_frm, true).setInfo("Error", ex.getLocalizedMessage());
            }
        }
    }

    private void myInit() {
        // disable btnConvert and ViewList
        btnConvert.setEnabled(false);
        tbViewList.setEnabled(false);
        mniViewList.setEnabled(false);
        trayViewList.setEnabled(false);
        mniQuickConverter.setEnabled(false);
        tbQuickConverter.setEnabled(false);
        _frm.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

        // check options
        checkOptions();

        _frm.setResizable(false);
        _frm.setIconImage(Sets.icon);
        try {
            maps.fillCMap();
            maps.fillVMap();
        } catch (Exception ex) {
            new FrmInfo(_frm, true).setInfo("Error", "Error loading currency map! Error: " + ex.getLocalizedMessage());
        }

        // show last update
        showLastUpdate();

        // set size
        _frm.setSize(new Dimension(300, 440));
        _frm.setPreferredSize(new Dimension(300, 440));

        _frm.setLocationRelativeTo(null);
        fillCmb();
        table.setGridColor(Color.darkGray);
        table.setSelectionBackground(Color.black);
        table.setSelectionForeground(Color.lightGray);

        // make tray icon - ChangeLog 04.08.2009
        if (SystemTray.isSupported()) {
            SystemTray systemTray = SystemTray.getSystemTray();
            MouseListener mouseListener = new MouseListener() {

                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {
                        if (_frm.isVisible()) {
                            _frm.setVisible(false);
                        } else {
                            _frm.setVisible(true);
                        }
                    }
                }

                public void mousePressed(MouseEvent e) {
                }

                public void mouseReleased(MouseEvent e) {
                }

                public void mouseEntered(MouseEvent e) {
                }

                public void mouseExited(MouseEvent e) {
                }
            };
            // action listeners
            ActionListener _show_hide = new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if (_frm.isVisible()) {
                        _frm.setVisible(false);
                    } else {
                        _frm.setVisible(true);
                    }
                }
            };// showHide
            ActionListener _exit = new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    System.out.print("Ciao ...");
                    System.exit(0);
                }
            };// exit
            ActionListener _update = new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    showUpdate();
                }
            };

            ActionListener _viewlist = new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    showViewList();
                }
            };// showHide

            trayUpdate.addActionListener(_update);
            trayShowHide.addActionListener(_show_hide);
            trayExit.addActionListener(_exit);
            trayViewList.addActionListener(_viewlist);

            trayIcon = new TrayIcon(Sets.icon, "Currency converter", popup);
            trayIcon.setImageAutoSize(true);
            trayIcon.addMouseListener(mouseListener);

            try {
                systemTray.add(trayIcon);
            } catch (Exception ex) {
                //System.out.print(ex.getLocalizedMessage());
            }
        } else {
            System.out.print("SystemTrayIcon is not supported ...");
        }
        //pack all this
        _frm.pack();
    }// myInit

    public void fillCmb() {
        ArrayList arrl = new ArrayList();
        arrl.addAll(Maps._cmap.keySet());
        Map cmap = Maps._cmap;
        Collections.sort(arrl);
        DefaultComboBoxModel dcbm = (DefaultComboBoxModel) cmbCurrency.getModel();
        for (Object one : arrl) {
            dcbm.addElement(cmap.get(one) + " (" + one + ")");
        }
    }

    public void showLastUpdate() {
        if (maps.getLastUpdate().length() != 0) {
            try {
                lblLastUpdate.setText("Last update : " + maps.getLastUpdate());
            } catch (Exception ex) {
                new FrmInfo(_frm, true).setInfo("Error", "Error reading file : " + ex.getLocalizedMessage());
            }
        } else {
            lblLastUpdate.setText("Last update : Never");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlMain = new javax.swing.JPanel();
        lblAmount = new javax.swing.JLabel();
        txtAmount = new javax.swing.JFormattedTextField();
        lblCurrency = new javax.swing.JLabel();
        cmbCurrency = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        btnConvert = new javax.swing.JButton();
        ToolBar = new javax.swing.JToolBar();
        tbUpdate = new javax.swing.JButton();
        tbExport = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        tbViewList = new javax.swing.JButton();
        tbQuickConverter = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        tbOptions = new javax.swing.JButton();
        mnbMain = new javax.swing.JMenuBar();
        mniFile = new javax.swing.JMenu();
        mniUpdate = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        mniExportToCSV = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        mniExit = new javax.swing.JMenuItem();
        mnView = new javax.swing.JMenu();
        mniViewList = new javax.swing.JMenuItem();
        mniQuickConverter = new javax.swing.JMenuItem();
        sep1 = new javax.swing.JSeparator();
        mniOptions = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        pnlStatusBar = new javax.swing.JPanel();
        lblLastUpdate = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        popup = new java.awt.PopupMenu();
        trayShowHide = new java.awt.MenuItem();
        trayQuick = new java.awt.MenuItem();
        trayUpdate = new java.awt.MenuItem();
        trayViewList = new java.awt.MenuItem();
        trayExit = new java.awt.MenuItem();

        pnlMain.setName("pnlMain"); // NOI18N
        pnlMain.setPreferredSize(new java.awt.Dimension(300, 400));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cconverter.CConverterApp.class).getContext().getResourceMap(FrmMain.class);
        lblAmount.setText(resourceMap.getString("lblAmount.text")); // NOI18N
        lblAmount.setName("lblAmount"); // NOI18N

        txtAmount.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        txtAmount.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtAmount.setText(resourceMap.getString("txtAmount.text")); // NOI18N
        txtAmount.setName("txtAmount"); // NOI18N
        txtAmount.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtAmountMouseClicked(evt);
            }
        });
        txtAmount.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtAmountKeyReleased(evt);
            }
        });

        lblCurrency.setText(resourceMap.getString("lblCurrency.text")); // NOI18N
        lblCurrency.setName("lblCurrency"); // NOI18N

        cmbCurrency.setName("cmbCurrency"); // NOI18N
        cmbCurrency.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbCurrencyItemStateChanged(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "...", "Currency", "Rate"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        table.setIntercellSpacing(new java.awt.Dimension(2, 2));
        table.setName("table"); // NOI18N
        jScrollPane1.setViewportView(table);
        table.getColumnModel().getColumn(0).setMinWidth(18);
        table.getColumnModel().getColumn(0).setPreferredWidth(18);
        table.getColumnModel().getColumn(0).setMaxWidth(18);
        table.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("table.columnModel.title0")); // NOI18N
        table.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("table.columnModel.title1")); // NOI18N
        table.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("table.columnModel.title2")); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cconverter.CConverterApp.class).getContext().getActionMap(FrmMain.class, this);
        btnConvert.setAction(actionMap.get("Convert")); // NOI18N
        btnConvert.setText(resourceMap.getString("btnConvert.text")); // NOI18N
        btnConvert.setToolTipText(resourceMap.getString("btnConvert.toolTipText")); // NOI18N
        btnConvert.setName("btnConvert"); // NOI18N

        ToolBar.setFloatable(false);
        ToolBar.setRollover(true);
        ToolBar.setName("ToolBar"); // NOI18N

        tbUpdate.setAction(actionMap.get("showUpdate")); // NOI18N
        tbUpdate.setIcon(resourceMap.getIcon("tbUpdate.icon")); // NOI18N
        tbUpdate.setText(resourceMap.getString("tbUpdate.text")); // NOI18N
        tbUpdate.setToolTipText(resourceMap.getString("tbUpdate.toolTipText")); // NOI18N
        tbUpdate.setFocusable(false);
        tbUpdate.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbUpdate.setName("tbUpdate"); // NOI18N
        tbUpdate.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        ToolBar.add(tbUpdate);

        tbExport.setAction(actionMap.get("exportTxt")); // NOI18N
        tbExport.setIcon(resourceMap.getIcon("tbExport.icon")); // NOI18N
        tbExport.setText(resourceMap.getString("tbExport.text")); // NOI18N
        tbExport.setToolTipText(resourceMap.getString("tbExport.toolTipText")); // NOI18N
        tbExport.setFocusable(false);
        tbExport.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbExport.setName("tbExport"); // NOI18N
        tbExport.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        ToolBar.add(tbExport);

        jSeparator1.setName("jSeparator1"); // NOI18N
        ToolBar.add(jSeparator1);

        tbViewList.setAction(actionMap.get("showViewList")); // NOI18N
        tbViewList.setIcon(resourceMap.getIcon("tbViewList.icon")); // NOI18N
        tbViewList.setText(resourceMap.getString("tbViewList.text")); // NOI18N
        tbViewList.setToolTipText(resourceMap.getString("tbViewList.toolTipText")); // NOI18N
        tbViewList.setFocusable(false);
        tbViewList.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbViewList.setName("tbViewList"); // NOI18N
        tbViewList.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        ToolBar.add(tbViewList);

        tbQuickConverter.setAction(actionMap.get("showQuickConverter")); // NOI18N
        tbQuickConverter.setIcon(resourceMap.getIcon("tbQuickConverter.icon")); // NOI18N
        tbQuickConverter.setText(resourceMap.getString("tbQuickConverter.text")); // NOI18N
        tbQuickConverter.setToolTipText(resourceMap.getString("tbQuickConverter.toolTipText")); // NOI18N
        tbQuickConverter.setFocusable(false);
        tbQuickConverter.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbQuickConverter.setName("tbQuickConverter"); // NOI18N
        tbQuickConverter.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        ToolBar.add(tbQuickConverter);

        jSeparator4.setName("jSeparator4"); // NOI18N
        ToolBar.add(jSeparator4);

        tbOptions.setAction(actionMap.get("showOptions")); // NOI18N
        tbOptions.setIcon(resourceMap.getIcon("tbOptions.icon")); // NOI18N
        tbOptions.setText(resourceMap.getString("tbOptions.text")); // NOI18N
        tbOptions.setToolTipText(resourceMap.getString("tbOptions.toolTipText")); // NOI18N
        tbOptions.setFocusable(false);
        tbOptions.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbOptions.setName("tbOptions"); // NOI18N
        tbOptions.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        ToolBar.add(tbOptions);

        javax.swing.GroupLayout pnlMainLayout = new javax.swing.GroupLayout(pnlMain);
        pnlMain.setLayout(pnlMainLayout);
        pnlMainLayout.setHorizontalGroup(
            pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
            .addGroup(pnlMainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblCurrency)
                    .addComponent(lblAmount))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtAmount, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                    .addComponent(cmbCurrency, 0, 178, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlMainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                    .addComponent(btnConvert))
                .addContainerGap())
        );
        pnlMainLayout.setVerticalGroup(
            pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMainLayout.createSequentialGroup()
                .addComponent(ToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblAmount)
                    .addComponent(txtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCurrency)
                    .addComponent(cmbCurrency, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnConvert)
                .addContainerGap())
        );

        mnbMain.setName("mnbMain"); // NOI18N

        mniFile.setText(resourceMap.getString("mniFile.text")); // NOI18N
        mniFile.setName("mniFile"); // NOI18N

        mniUpdate.setAction(actionMap.get("showUpdate")); // NOI18N
        mniUpdate.setIcon(resourceMap.getIcon("mniUpdate.icon")); // NOI18N
        mniUpdate.setText(resourceMap.getString("mniUpdate.text")); // NOI18N
        mniUpdate.setName("mniUpdate"); // NOI18N
        mniFile.add(mniUpdate);

        jMenu2.setIcon(resourceMap.getIcon("jMenu2.icon")); // NOI18N
        jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N
        jMenu2.setName("jMenu2"); // NOI18N

        mniExportToCSV.setAction(actionMap.get("exportTxt")); // NOI18N
        mniExportToCSV.setIcon(resourceMap.getIcon("mniExportToCSV.icon")); // NOI18N
        mniExportToCSV.setText(resourceMap.getString("mniExportToCSV.text")); // NOI18N
        mniExportToCSV.setName("mniExportToCSV"); // NOI18N
        jMenu2.add(mniExportToCSV);

        mniFile.add(jMenu2);

        jSeparator3.setName("jSeparator3"); // NOI18N
        mniFile.add(jSeparator3);

        mniExit.setAction(actionMap.get("quit")); // NOI18N
        mniExit.setText(resourceMap.getString("mniExit.text")); // NOI18N
        mniExit.setName("mniExit"); // NOI18N
        mniFile.add(mniExit);

        mnbMain.add(mniFile);

        mnView.setText(resourceMap.getString("mnView.text")); // NOI18N
        mnView.setName("mnView"); // NOI18N

        mniViewList.setAction(actionMap.get("showViewList")); // NOI18N
        mniViewList.setIcon(resourceMap.getIcon("mniViewList.icon")); // NOI18N
        mniViewList.setText(resourceMap.getString("mniViewList.text")); // NOI18N
        mniViewList.setName("mniViewList"); // NOI18N
        mnView.add(mniViewList);

        mniQuickConverter.setAction(actionMap.get("showQuickConverter")); // NOI18N
        mniQuickConverter.setIcon(resourceMap.getIcon("mniQuickConverter.icon")); // NOI18N
        mniQuickConverter.setText(resourceMap.getString("mniQuickConverter.text")); // NOI18N
        mniQuickConverter.setName("mniQuickConverter"); // NOI18N
        mnView.add(mniQuickConverter);

        sep1.setName("sep1"); // NOI18N
        mnView.add(sep1);

        mniOptions.setAction(actionMap.get("showOptions")); // NOI18N
        mniOptions.setIcon(resourceMap.getIcon("mniOptions.icon")); // NOI18N
        mniOptions.setText(resourceMap.getString("mniOptions.text")); // NOI18N
        mniOptions.setName("mniOptions"); // NOI18N
        mnView.add(mniOptions);

        mnbMain.add(mnView);

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        jMenuItem1.setAction(actionMap.get("showAbout")); // NOI18N
        jMenuItem1.setIcon(resourceMap.getIcon("jMenuItem1.icon")); // NOI18N
        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenu1.add(jMenuItem1);

        mnbMain.add(jMenu1);

        pnlStatusBar.setName("pnlStatusBar"); // NOI18N
        pnlStatusBar.setPreferredSize(new java.awt.Dimension(562, 25));

        lblLastUpdate.setText(resourceMap.getString("lblLastUpdate.text")); // NOI18N
        lblLastUpdate.setName("lblLastUpdate"); // NOI18N

        jSeparator2.setName("jSeparator2"); // NOI18N

        javax.swing.GroupLayout pnlStatusBarLayout = new javax.swing.GroupLayout(pnlStatusBar);
        pnlStatusBar.setLayout(pnlStatusBarLayout);
        pnlStatusBarLayout.setHorizontalGroup(
            pnlStatusBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlStatusBarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblLastUpdate)
                .addContainerGap(288, Short.MAX_VALUE))
            .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
        );
        pnlStatusBarLayout.setVerticalGroup(
            pnlStatusBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlStatusBarLayout.createSequentialGroup()
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblLastUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        popup.setFont(resourceMap.getFont("popup.font")); // NOI18N
        popup.setLabel(resourceMap.getString("popup.label")); // NOI18N

        trayShowHide.setActionCommand(resourceMap.getString("trayShowHide.actionCommand")); // NOI18N
        trayShowHide.setFont(resourceMap.getFont("trayShowHide.font")); // NOI18N
        trayShowHide.setLabel(resourceMap.getString("trayShowHide.label")); // NOI18N
        popup.add(trayShowHide);
        popup.addSeparator();
        trayQuick.setLabel(resourceMap.getString("trayQuick.label")); // NOI18N
        trayQuick.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trayQuickActionPerformed(evt);
            }
        });
        popup.add(trayQuick);
        popup.addSeparator();
        trayUpdate.setLabel(resourceMap.getString("trayUpdate.label")); // NOI18N
        popup.add(trayUpdate);

        trayViewList.setLabel(resourceMap.getString("trayViewList.label")); // NOI18N
        popup.add(trayViewList);
        popup.addSeparator();
        trayExit.setLabel(resourceMap.getString("trayExit.label")); // NOI18N
        popup.add(trayExit);

        setComponent(pnlMain);
        setMenuBar(mnbMain);
        setStatusBar(pnlStatusBar);
        setToolBar(ToolBar);
    }// </editor-fold>//GEN-END:initComponents

    private void trayQuickActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_trayQuickActionPerformed
        // TODO add your handling code here:
        this.showQuickConverter();
    }//GEN-LAST:event_trayQuickActionPerformed

    private void txtAmountMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtAmountMouseClicked
        // TODO add your handling code here:
        txtAmount.setFocusable(true);
        txtAmount.selectAll();
    }//GEN-LAST:event_txtAmountMouseClicked

    public int countIndexOf(String text, String search) {
        int count = 0;
        for (int fromIndex = 0; fromIndex > -1; count++) {
            fromIndex = text.indexOf(search, fromIndex + ((count > 0) ? 1 : 0));
        }
        return count - 1;
    }

    private void txtAmountKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAmountKeyReleased
        // TODO add your handling code here:
        runConvert();
    }//GEN-LAST:event_txtAmountKeyReleased

    private void cmbCurrencyItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbCurrencyItemStateChanged
        // TODO add your handling code here:
        runConvert();
    }//GEN-LAST:event_cmbCurrencyItemStateChanged

    private void runConvert(){
        if (txtAmount.getText().length() > 0 && !Maps._vmap.isEmpty()) {
            if (!txtAmount.getText().endsWith(".")) {// u slucaju da se ne zavrsava na .
                if (this.countIndexOf(txtAmount.getText(), ".") <= 1) {// ako ima jednu ili nijednu tacku
                    this.Convert();
                } else {
                    txtAmount.setText("");
                    new FrmInfo(_frm, true).setInfo("Error", "Enter amount for converting...");
                }
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToolBar ToolBar;
    private javax.swing.JButton btnConvert;
    private javax.swing.JComboBox cmbCurrency;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JLabel lblAmount;
    private javax.swing.JLabel lblCurrency;
    private javax.swing.JLabel lblLastUpdate;
    private javax.swing.JMenu mnView;
    private javax.swing.JMenuBar mnbMain;
    private javax.swing.JMenuItem mniExit;
    private javax.swing.JMenuItem mniExportToCSV;
    private javax.swing.JMenu mniFile;
    private javax.swing.JMenuItem mniOptions;
    private javax.swing.JMenuItem mniQuickConverter;
    private javax.swing.JMenuItem mniUpdate;
    private javax.swing.JMenuItem mniViewList;
    private javax.swing.JPanel pnlMain;
    private javax.swing.JPanel pnlStatusBar;
    private java.awt.PopupMenu popup;
    private javax.swing.JSeparator sep1;
    private javax.swing.JTable table;
    private javax.swing.JButton tbExport;
    private javax.swing.JButton tbOptions;
    private javax.swing.JButton tbQuickConverter;
    private javax.swing.JButton tbUpdate;
    private javax.swing.JButton tbViewList;
    private java.awt.MenuItem trayExit;
    private java.awt.MenuItem trayQuick;
    private java.awt.MenuItem trayShowHide;
    private java.awt.MenuItem trayUpdate;
    private java.awt.MenuItem trayViewList;
    private javax.swing.JFormattedTextField txtAmount;
    // End of variables declaration//GEN-END:variables
}
